//
//  DownLoadVC.m
//  IntegralDog
//
//  Created by User on 16/1/22.
//  Copyright © 2016年 hanen. All rights reserved.
//

#import "DownLoadVC.h"
#import "AppDelegate.h"
#import "TabbarController.h"
#import "DownloadCell.h"
#import "DLContentnVC.h"
#import "Macro.h"
#import "SDCycleScrollView.h"
#import "PersonalNetTool.h"
#import "ProductModel.h"
#import "UIImageView+WebCache.h"
#define HEIGHT [[UIScreen mainScreen] bounds].size.height/480.0
#define WIDTH self.view.frame.size.width/320.0
@interface DownLoadVC ()
{
    UIButton *endButton;
    NSMutableArray *ProductArr;
    NSMutableArray *pidArr;
    UITableView *Tableview;
    NSMutableArray *_Arr;
    NSMutableArray *_JiFenArr;
    NSTimer *timer;
    UILabel *name;
    UILabel *goal;
}

@end
int Index;
int i;
@implementation DownLoadVC

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
    self.tabBarController.hidesBottomBarWhenPushed = NO;
    [timer setFireDate:[NSDate distantPast]];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [timer setFireDate:[NSDate distantFuture]];
}

- (void)viewDidAppear:(BOOL)animated
{
    [self getProductListWithPage:1 Rows:20];
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}

- (void)getProductListWithPage:(int)page Rows:(int)rows
{
    [PersonalNetTool getProductListWithpage:page  rows:rows complationBlock:^(NSDictionary *dic) {
        [ProductArr removeAllObjects];
        [pidArr removeAllObjects];
        for (NSDictionary *arr in [dic objectForKey:@"rows"]) {
            [pidArr addObject:[arr objectForKey:@"id"]];
            [ProductArr addObject:arr];
        }
        [Tableview reloadData];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithFloat:WIDTH] forKey:@"width"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    ProductArr = [[NSMutableArray alloc] init];
    pidArr = [[NSMutableArray alloc] init];
    _Arr = [NSMutableArray arrayWithObjects:@"小红",@"小王",@"小小",@"小杰",@"小齐", nil];
    _JiFenArr = [NSMutableArray arrayWithObjects:@"500",@"300",@"200",@"350",@"650", nil];
    
    self.navigationItem.title = @"应用下载";
  
    [self creatTableView];
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake2(0, 0, 320, 20)];
    img.image = [UIImage imageNamed:@"Btn_Normal_Gongxi"];
    
    UIImageView *labaImg = [[UIImageView alloc] init];
    labaImg.center = CGPointMake(37, img.center.y);
    labaImg.bounds = CGRectMake(0, 0, 12*HEIGHT, 12*HEIGHT);
    labaImg.image = [UIImage imageNamed:@"Btn_Normal_Gongxilaba"];
    
    UILabel *lab1 =[[UILabel alloc] init];
    lab1.center = CGPointMake(60*WIDTH, img.center.y);
    lab1.bounds = CGRectMake(0, 0, 29*WIDTH, 14*HEIGHT);
    lab1.text = @"恭喜";
    lab1.textColor = [UIColor whiteColor];
    
    UILabel *lab2 = [[UILabel alloc] init];
    name = [[UILabel alloc] init];
    name.center = CGPointMake(lab1.center.x+lab1.frame.size.width/2.0+18, img.center.y);
    name.bounds = CGRectMake(0, 0, 29*WIDTH, 14*HEIGHT);
    name.text = @"小明";

    lab2.center = CGPointMake(name.center.x+name.frame.size.width/2.0+5+91, img.center.y);
    lab2.bounds = CGRectMake(0, 0, 186, 14*HEIGHT);
    lab2.textColor = [UIColor whiteColor];
    lab2.text = @"成功下载百度应用，积分累积到";
    
    goal = [[UILabel alloc] init];
    goal.center = CGPointMake(lab2.center.x+lab2.frame.size.width/2.0+8, img.center.y);
    goal.bounds = CGRectMake(0, 0, 29*WIDTH, 14*HEIGHT);
    goal.text = @"300";
    
    [self setFontLabel:name byscale:WIDTH];
    [self setFontLabel:lab1 byscale:WIDTH];
    [self setFontLabel:lab2 byscale:WIDTH];
    [self setFontLabel:goal byscale:WIDTH];
    name.textColor = mainOrangeColor;
    goal.textColor = mainOrangeColor;
    
    [self.view addSubview:img];
    [self.view addSubview:labaImg];
    [self.view addSubview:lab1];
    [self.view addSubview:lab2];
    [self.view addSubview:name];
    [self.view addSubview:goal];
    timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timerRun) userInfo:nil repeats:YES];
    [timer fire];
}

- (void)timerRun
{
    
    NSLog(@"%d",i);
    name.text = [_Arr objectAtIndex:i];
    goal.text = [_JiFenArr objectAtIndex:i];
    i++;
    if (i == 5) {
        i = 0;
    }

}

- (void)setFontLabel:(UILabel *)label  byscale:(CGFloat )width
{
    int size;
    if (width >1||width <1.29) {
        size = 12;
    }
    else
    {
        size = 14;
    }
    [label setFont:[UIFont boldSystemFontOfSize:size]];
}

- (void)creatTableView
{
    _headImageArr = [NSMutableArray arrayWithArray:@[[UIImage imageNamed:@"Btn_Normal_Xinchunjifen"],[UIImage imageNamed:@"Btn_Normal_Xinchunjifen"],[UIImage imageNamed:@"Btn_Normal_Xinchunjifen"]]];
    SDCycleScrollView *headScrollView = [[SDCycleScrollView alloc] initWithFrame:CGRectMake1(0, 20, 320, 100)];
    headScrollView.localizationImagesGroup  = self.headImageArr;
    headScrollView.pageControlDotSize = CGSizeMake(5, 5);
    headScrollView.autoScrollTimeInterval = 2.5;
    [self.view addSubview:headScrollView];

    UIImageView *xiazai = [[UIImageView alloc] initWithFrame:CGRectMake1(15, 125, 10, 10)];
    xiazai.image = [UIImage imageNamed:@"Btn_Normal_Xiazai2"];
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake1(32, 123, 100, 15)];
    title.text = @"大家都在下载";
    title.textColor = fontLightGrayColor;
    title.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:title];
    [self.view addSubview:xiazai];
    UILabel *xianlab = [[UILabel alloc] initWithFrame:CGRectMake1(0, 141.5, 320, 0.5)];
    Tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 142*HEIGHT, 320*WIDTH, self.view.frame.size.height-xianlab.frame.origin.y  - 60 - 64)];
    Tableview.delegate = self;
    Tableview.dataSource = self;
    Tableview.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    //[tableview registerNib:[UINib nibWithNibName:@"DownloadCell" bundle:nil] forCellReuseIdentifier:@"DownloadCell"];
    
    xianlab.backgroundColor = [UIColor colorWithRed:236.0 / 255 green:235.0/255 blue:232.0/255 alpha:1];
    [self.view addSubview:Tableview];
    [self.view addSubview:xianlab];
    
}

- (void)ButtonClick:(UIButton *)butt
{
    [endButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [butt setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    endButton = butt;
}

#pragma mark ------------------------UITableView Delegate-----------------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return ProductArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *str = @"DownloadCell";
     DownloadCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil){
    cell = [[DownloadCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    if (ProductArr.count != 0) {
        ProductModel *model = [[ProductModel alloc] initWithDictionary:[ProductArr objectAtIndex:indexPath.row]];

    [cell.TitleImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMAGEURL,model.logo]] placeholderImage:[UIImage imageNamed:@"Icon－60@3X"]];
    cell.AppName.text = model.title;
        cell.typeLab.text = model.classify;
        cell.DownCount.text = [NSString stringWithFormat:@"%ld",[model.downloadUsed integerValue]];
        cell.jifen.text = [NSString stringWithFormat:@"%ld",[model.reward integerValue]];
        //cell.starClass = (int)[model.starClass integerValue];
        [cell star:(int)[model.star_Class integerValue]];
        
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  49*HEIGHT;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    int a = (int)[[pidArr objectAtIndex:indexPath.row] integerValue];
    DLContentnVC *vc = [[DLContentnVC alloc] init];
    [vc setValue:[NSString stringWithFormat:@"%d",a] forKey:@"pid"];
    [self.navigationController pushViewController:vc animated:NO];
    NSLog(@"%@",pidArr);
}

CG_INLINE CGRect
CGRectMake1(CGFloat x,CGFloat y,CGFloat width,CGFloat height)
{
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    return CGRectMake(x * app.autoSizeScaleX, y * app.autoSizeScaleY, width * app.autoSizeScaleX, height * app.autoSizeScaleY);
}

CG_INLINE CGRect
CGRectMake2(CGFloat x,CGFloat y,CGFloat width,CGFloat height)
{
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    return CGRectMake(x , y , width * app.autoSizeScaleX, height * app.autoSizeScaleY);
}

@end